/**
 * 
 */
package net.zarubsys.unianalyzer.dataminers.lpt.wizard.utils;

import java.util.ArrayList;
import java.util.List;

import net.zarubsys.unianalyzer.dataminers.lpt.IMeasuringParametersWrapper;
import net.zarubsys.unianalyzer.dataminers.lpt.gui.parameterObject.LPTMeasuringSettings;
import net.zarubsys.unianalyzer.dataminers.lpt.gui.parameterObject.PortSettings;
import net.zarubsys.unianalyzer.dataminers.lpt.gui.parameterObject.StartStopData;
import net.zarubsys.unianalyzer.dataminers.lpt.internal.impl.Trigger;
import net.zarubsys.unianalyzer.dataminers.lpt.pin.CustomizedPin;
import net.zarubsys.unianalyzer.dataminers.lpt.triggers.ITrigger;

/**
 * ParameterObjectParser
 *
 * @author  &lt;A HREF=&quot;mailto:lukas.zaruba@media-solutions.cz&quot;&gt;Lukas Zaruba&lt;/A&gt;, MEDIA SOLUTIONS CZECH REPUBLIC Ltd.
 * @version $Revision$ $Date$
 */
public class ParameterObjectParser implements IMeasuringParametersWrapper {
	
	private LPTMeasuringSettings parameterObject;

	public ParameterObjectParser(LPTMeasuringSettings parameterObject) {
		this.parameterObject = parameterObject;
	}
	
	public LPTMeasuringSettings getParameterObject() {
		return parameterObject;
	}
	
	
	public int getAddress() {
		PortSettings portSettings = parameterObject.getPortSettings();
		int address = portSettings.getPortNumber() + portSettings.getPortOffset();
		return address;
	}

	
	public int getFrequency() { 
		return parameterObject.getPortSettings().getFrequency();
	}

	public int getSamplesCount() {
		StartStopData startStopData = parameterObject.getStartStopData();
		if (startStopData.isStopByTime()) {
			float timeInSec = (startStopData.getMeasureTime() * startStopData.getMeasureTimeUnits()) / 1000000f; 
			int sampleCount = Math.round(timeInSec * parameterObject.getPortSettings().getFrequency() * parameterObject.getPortSettings().getUnitsComboValue()); 
			return sampleCount;
		} else {
			return startStopData.getSamplesCount();
		}
	}

	
	public ITrigger getTrigger() {
		StartStopData startStopData = parameterObject.getStartStopData();
		if (!startStopData.isUseStartTrigger()) {
			return null;
		}
		
		int mask = (int) Math.pow(2, startStopData.getStartTriggerPin().getNumber());
		
		int resultBase = 0;
		if (!startStopData.isRisingEdge()) {
			resultBase = ~resultBase;
		}
		int firstResult = mask & resultBase;
		int secondResult = mask & ~resultBase;
		
		long maxWaitingIntNano = startStopData.getMaxWaitingInterval() * startStopData.getMaxWaitingIntervalUnits() * 1000;
		
		ITrigger trigger = new Trigger(mask, firstResult, secondResult, maxWaitingIntNano);
		return trigger;
	}
	
	
	public String getFrequencyUnits() {
		return parameterObject.getPortSettings().getUnitsComboValueDescription();
	}

	
	public long getMaxWaitingInterval() {
		StartStopData startStopData = parameterObject.getStartStopData();
		return startStopData.getMaxWaitingInterval();
	}

	
	public CustomizedPin getTriggerPin() {
		StartStopData startStopData = parameterObject.getStartStopData();
		return startStopData.getStartTriggerPin();
	}

	
	public boolean getTriggerRisingEdge() {
		StartStopData startStopData = parameterObject.getStartStopData();
		return startStopData.isRisingEdge();
	}
	
	public int getResultMask() {
		List<CustomizedPin> cPins = parameterObject.getCustomizedPins();
		int i = 0;
		int result = 0;
		for (CustomizedPin cPin : cPins) {
			if (cPin.isChecked()) {
				result |= (int) Math.pow(2, i);
			}
			i++;
		}
		return result;
	}
	
	public List<String> getPinDescs() {
		List<CustomizedPin> cPins = parameterObject.getCustomizedPins();
		List<String> names = new ArrayList<String>();
		int i = 0;
		for (CustomizedPin cPin : cPins) {
			String cName = cPin.getCustomizedName();
			if (cName.length() > 0) {
				names.add(i + " - " + cName);
			} else {
				names.add(i + " - (" + cPin.getDescription() + ")");
			}
			i++;
		}
		return names;
	}

}


/*
 * $Log$
 */